//
//  UIImage+LYAdd.m
//  showtime
//
//  Created by liyp on 2016/12/6.
//  Copyright © 2016年 崔国冠. All rights reserved.
//

#import "UIImage+LYAdd.h"

@implementation UIImage (LYAdd)
- (UIImage*)renderOriginal{
    return [self imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
- (UIImage*)renderTemplate{
    return [self imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
- (UIImage*)renderAutomatic{
    return [self imageWithRenderingMode:UIImageRenderingModeAutomatic];
}

#pragma mark - 返回某种颜色图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithColors:(NSArray<UIColor*>*)colors positions:(NSArray<NSNumber*>*)positions size:(CGSize)size{
    if (size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CFMutableArrayRef colorArray = CFArrayCreateMutable(NULL, colors.count, NULL);
    for (UIColor *color in colors) {
        CFArrayAppendValue(colorArray, color.CGColor);
    }
    
    CGFloat postionArray[positions.count] ;
    for (int i = 0 ; i < positions.count ; i++) {
        postionArray[i] = positions[i].floatValue;
    }
    
    CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpace, colorArray, postionArray);

    
    CGColorSpaceRelease(colorSpace);
    CFRelease(colorArray);
    
    
    CGContextDrawLinearGradient(context, gradientRef, CGPointMake(size.width/2.0, 0), CGPointMake(size.width/2.0, size.height), kCGGradientDrawsBeforeStartLocation);
    
    CGGradientRelease(gradientRef);
    
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+(UIImage *)compressImageWith:(UIImage *)image
{
    float imageWidth = image.size.width;
    float imageHeight = image.size.height;
    float width = 640;
    float height = image.size.height/(image.size.width/width);
    
    float widthScale = imageWidth /width;
    float heightScale = imageHeight /height;
    
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    if (widthScale > heightScale) {
        [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
    }
    else {
        [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
    }
    
    // 从当前context中创建一个改变大小后的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

@end
